package cn.wu.reggie.controller;

import cn.wu.reggie.common.R;
import cn.wu.reggie.dto.SetmealDto;
import cn.wu.reggie.service.SetmealService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    // 清除SETMEAL下的所有key
    @CacheEvict(value = "SETMEAL",allEntries = true)
    public R save(@RequestBody SetmealDto setmealDto, HttpSession session){
        // 1 封装
        Long userId = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(userId);
        setmealDto.setUpdateUser(userId);

        // 2 调用
        return setmealService.save(setmealDto);

    }

    @GetMapping("page")
    public R findByPage(@RequestParam("page")Integer pageNum,Integer pageSize,String name){
        return setmealService.findByPage(pageNum,pageSize,name);
    }

    @DeleteMapping
    public R delete(Long[] ids){
        return setmealService.delete(ids);
    }


    @GetMapping("list")
    @Cacheable(value = "SETMEAL",key = "'ONSALE:'+#categoryId",unless = "#result.data.size()==0") // 当R的返回值data数据为空时不缓存，比如商务分类下没有对应的套餐
    public R findOnSaleByCategoryId(Long categoryId,Integer status){
        return setmealService.findOnSaleByCategoryId(categoryId,status);
    }

}
